package com.kfm.base.arrays;

import java.util.Scanner;

public class BinarySearch {

    public static void main(String[] args) {
        /*
            二分查找： 已排序好的数组
         */

        int[] arr  = {1, 2, 3, 4, 5, 6};
        Scanner in = new Scanner(System.in);
        int num = in.nextInt(); // 查找的数

        int left = 0;
        int right = arr.length - 1;
        int index = -1;

        while(left <= right){
            int middle = (left + right) / 2; // 中间的下标
            if (num == arr[middle]){ // 值和要找的值一样
                index = middle; // 找到了
                break;
            } else if (num > arr[middle]) {
                left = middle + 1;
            } else {
                right = middle - 1;
            }
        }

        System.out.println(index);
    }
}
